package com.example.stars_project_android.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.TextView;

import androidx.fragment.app.Fragment;

import com.example.stars_project_android.R;
import com.example.stars_project_android.adapter.GvAdapter;
import com.example.stars_project_android.entity.SpaceTime;
import com.example.stars_project_android.index.CallRoom;
import com.example.stars_project_android.index.MovieRoom;
import com.example.stars_project_android.index.MusicRoom;
import com.example.stars_project_android.index.RoomDetail;
import com.example.stars_project_android.util.Constant;
import com.hyphenate.EMValueCallBack;
import com.hyphenate.chat.EMChatRoom;
import com.hyphenate.chat.EMClient;
import com.hyphenate.chat.EMMessage;
import com.hyphenate.easeui.constants.EaseConstant;

import java.util.ArrayList;
import java.util.List;

/**
 * 首页各个时空的fragment（收藏时空、聊天时空……）
 */
public class HomePageFragment extends Fragment {

    private static final String ARG_PARAM1 = "param1";

    private String mParam1;

    private TextView textView;
    private GridView gridView;
    private View rootView;
    //数据源
    private List<SpaceTime> collectionsList = new ArrayList<>();
    //适配器
    private GvAdapter gvAdapter;

    public HomePageFragment() {

    }

    public static HomePageFragment newInstance(String param1) {
        HomePageFragment fragment = new HomePageFragment();
        Bundle args = new Bundle();
        args.putString(ARG_PARAM1, param1);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            mParam1 = getArguments().getString(ARG_PARAM1);
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
//        View rootView = inflater.inflate(R.layout.layout_fragment, container, false);
        if (null == rootView) {
            rootView = inflater.inflate(R.layout.layout_fragment, container,
                    false);
            initView(rootView);
        }
//        gridView = rootView.findViewById(R.id.grid_view);
        ViewGroup parent = (ViewGroup) rootView.getParent();
        if (null != parent) {
            parent.removeView(rootView);
        }
        return rootView;
    }

    @Override
    public void onResume() {
        super.onResume();
            //如果不是第一次加载，刷新数据
            initView(rootView);
            collectionsList = Constant.roomList;
    }


    private void initView(View rootView) {
        initTuijian(rootView);
    }

    private void initTuijian(View rootView2) {
        gridView = (GridView) rootView.findViewById(R.id.grid_view);
        // 数据源
        collectionsList = Constant.roomList;
//        collectionsList = setData();
        // 适配器
        gvAdapter = new GvAdapter(
                getActivity(),
                R.layout.myroom_item,
                collectionsList
        );
        // 添加控件适配器
        gridView.setAdapter(gvAdapter);
        // 添加GridView的监听事件
        gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, View view,
                                    int position, long id) {

                switch (mParam1){
                    case "聊天时空":
                        joinChatRoom(position,RoomDetail.class);
                        break;
                    case "连麦时空":
                        joinChatRoom(position,CallRoom.class);
                        break;
//                    case "学习时空":
//                        break;
                    case "音乐时空":
                        joinChatRoom(position, MusicRoom.class);
                        break;
                    case "看剧时空":
                        joinChatRoom(position, MovieRoom.class);
                        break;
                    default:
                        break;
                }

            }
        });
    }

    private List<SpaceTime> setData() {
        List<SpaceTime> collections = new ArrayList<>();
        SpaceTime data1 = new SpaceTime();
        data1.setRoomName("万圣节一起挂机啊");
        collections.add(data1);
        SpaceTime data2 = new SpaceTime();
        data2.setRoomName("聊天时空");
        collections.add(data2);
        SpaceTime data3 = new SpaceTime();
        data3.setRoomName("聊天时空");
        collections.add(data3);
        SpaceTime data4 = new SpaceTime();
        data4.setRoomName("聊天时空");
        collections.add(data4);
        SpaceTime data5 = new SpaceTime();
        data5.setRoomName("聊天时空");
        collections.add(data5);
        return collections;
    }
    private void joinChatRoom(int position,Class room){
        EMClient.getInstance().chatroomManager().joinChatRoom(collectionsList.get(position).getRoomId(), new EMValueCallBack<EMChatRoom>() {
            @Override
            public void onSuccess(EMChatRoom value) {
                System.out.println("我TM进去聊天室了=============================");
                Intent intent = new Intent(getActivity(),
                        room);
                System.out.println("这里应该跳转啊我朝");
                intent.putExtra(EaseConstant.EXTRA_CONVERSATION_ID,collectionsList.get(position).getRoomId());
                intent.putExtra(EaseConstant.EXTRA_IS_ROAM,true);
                intent.putExtra(EaseConstant.EXTRA_CHAT_TYPE, EMMessage.ChatType.GroupChat);
                startActivity(intent);
            }

            @Override
            public void onError(int error, String errorMsg) {
                System.out.println("没有进去聊天室啊啊啊啊===============");
            }
        });
    }

}


